package beautiful.butterfly.drds.binlog.event.data;


import beautiful.butterfly.drds.binlog.event.EventHeader;
import beautiful.butterfly.drds.binlog.event.RowEventData;
import beautiful.butterfly.drds.binlog.utils.Constants;
import beautiful.butterfly.drds.binlog.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.*;

public class DeleteRowsEventData implements RowEventData
{


    private static final long serialVersionUID = -3478409831005413055L;
    @Setter
    @Getter
    private long tableId;
    // private BitSet includedColumns;
    @Setter
    @Getter
    private String[] includedColumnNames;
    @Setter
    @Getter
    private List<Serializable[]> rows;
    @Setter
    @Getter
    private EventHeader eventHeader;
    @Setter
    @Getter
    private TableMapEventData tableMapEventData;
    @Setter
    @Getter
    private String databaseName;
    @Setter
    @Getter
    private String tableName;

    public DeleteRowsEventData()
    {

    }


    public void setIncludedColumns(BitSet includedColumns, HashMap<String, String> mappings)
    {
        // this.includedColumns = includedColumns;
        this.includedColumnNames = StringUtils.map(includedColumns, mappings);
    }


    @Override
    public String toString()
    {
        final StringBuilder sb = new StringBuilder();
        sb.append("DeleteRowsEventData");
        sb.append("{tableId=").append(tableId);
        sb.append(", includedColumns=").append(Arrays.toString(includedColumnNames));
        sb.append(", rows=[");
        for (Object[] row : rows)
        {
            sb.append("\n     ").append(Arrays.toString(row)).append(",");
        }
        if (!rows.isEmpty())
        {
            sb.replace(sb.length() - 1, sb.length(), "\n");
        }
        sb.append("]}");
        return sb.toString();
    }

    @Override
    public void setEventHeader(EventHeader eventHeader)
    {
        this.eventHeader = eventHeader;
    }

    @Override
    public void setTableMapEventData(TableMapEventData t)
    {
        tableMapEventData = t;
        databaseName = tableMapEventData.getDatabaseName();
        tableName = tableMapEventData.getTableName();
    }

    @Override
    public ArrayList<JSONObject> toJson()
    {
        ArrayList<JSONObject> result = new ArrayList<JSONObject>();

        // 准备工作1
        String[] columns = this.includedColumnNames;
        int length;
        for (Serializable[] row : rows)
        {
            // 遍历每一个row
            JSONObject object = new JSONObject();
            // strBuilder.append("\"" + Constants.UUID + "\":\"" + uuid +
            // "\",");
            object.put(Constants.database_name, databaseName);
            object.put(Constants.table_name, tableName);
            object.put(Constants.ACTION_TYPE, Constants.ACTION_DELETE);
            object.put(Constants.ACTION_TIME, eventHeader.getTimestamp());
            length = row.length;
            for (int index = 0; index < length; index++)
            {
                object.put(columns[index], row[index]);
            }
            result.add(object);
            // LoggerUtils.debug(logger, strBuilder.toString());
        }
        return result;
    }

    @Override
    public String getDatabaseName()
    {
        return databaseName;
    }

    @Override
    public String getTableName()
    {
        return tableName;
    }

}
